package com.example.service.impl;

import com.example.mapper.UserMapper;
import com.example.pojo.ArticleInfo;
import com.example.pojo.User;
import com.example.pojo.admin.SearchBean;
import com.example.service.UserService;
import com.example.utils.Md5Util;
import com.example.utils.RandomUtil;
import com.example.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.time.DayOfWeek;
import java.time.Instant;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description TODO
 * @Author fantasy
 * @Date 2024/6/26 22:09
 **/

@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Override
    public User findByUsername(String username) {
        return userMapper.findByUsername(username);
    }

    @Override
    public void register(String username, String password) {
        String md5Encrypt = Md5Util.md5Encrypt(password);
        userMapper.registerUser(new User(username, md5Encrypt));
    }

    @Override
    public void update(User user) {
        userMapper.update(user);
    }

    @Override
    public void updateAvatar(String avatar) {
        Map<String, Object> o = ThreadLocalUtil.get();
        Integer id = (Integer) o.get("id");

        userMapper.updateAvatar(avatar,id);
    }
    @Override
    public List getArticleInfo(Integer id) {
        return userMapper.getArticleInfo(id);
    }

    @Override
    public List getDayInfo(Integer id) {
        ArrayList list = new ArrayList();
        List<LocalDate> weekDates = getThisWeekDates();
        HashMap map = new HashMap();
        map.put("start",weekDates.get(0));
//        map.put("start","2019-01-30");
        map.put("end",weekDates.get(6));
        map.put("id",id);
        List<ArticleInfo> dayInfo = userMapper.getDayInfo(map);
        for (int i = 0; i < 7; i++) {
            LocalDate date = weekDates.get(i);
            for (int j = 0; j < dayInfo.size(); j++) {
                ArticleInfo info = dayInfo.get(j);
                if (date.toString().equals(info.getName())) {
                    list.add(info.getValue().toString());
                }
            }
            if (list.size() <= i){
                list.add(i,"0");
            }
        }
        return list;
    }
    @Override
    public List getDayInfo() {
        ArrayList list = new ArrayList();
        List<LocalDate> weekDates = getThisWeekDates();
        HashMap map = new HashMap();
        map.put("start",weekDates.get(0));
//        map.put("start","2019-01-30");
        map.put("end",weekDates.get(6));
        List<ArticleInfo> dayInfo = userMapper.getDayInfo(map);
        for (int i = 0; i < 7; i++) {
            LocalDate date = weekDates.get(i);
            for (int j = 0; j < dayInfo.size(); j++) {
                ArticleInfo info = dayInfo.get(j);
                if (date.toString().equals(info.getName())) {
                    list.add(info.getValue().toString());
                }
            }
            if (list.size() <= i){
                list.add(i,"0");
            }
        }
        return list;
    }

    @Override
    public List<User> getUserList() {
        return userMapper.getUserList();
    }

    @Override
    public User getUserById(Integer id) {
        User user = userMapper.getUserById(id);
        String pic = user.getUserPic();
        File file;
        try {
            file = new File("D:\\studyJava\\ideaProject\\springboot-aliyun-start\\big-event\\uploadfile\\" + pic);
            FileInputStream fis = new FileInputStream(file);
            byte[] bytes = new byte[fis.available()];
            fis.read(bytes);
            user.setUserPic(new String(bytes));
            fis.close();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return user;
    }

    @Override
    public List<User> getUserLikeList(SearchBean searchBean) {
        return userMapper.getUserLikeList(searchBean);
    }

    @Override
    public void addUser(User user) {
        String string = RandomUtil.getRandomString(10)+ Instant.now().getEpochSecond();
        String img = user.getUserPic();
        File file;
        try {
            file = new File("D:\\studyJava\\ideaProject\\springboot-aliyun-start\\big-event\\uploadfile\\" + string + ".png");
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(img.getBytes());
            fos.flush();
            fos.close();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        user.setUserPic(file.getName());
        user.setPassword(Md5Util.md5Encrypt(user.getPassword()));
        userMapper.addUser(user);
    }

    @Override
    public void updatePasswd(String passwd,Integer id) {
        userMapper.updatePasswd(passwd,id);
    }

    @Override
    public void delete(Integer id) {
        userMapper.delete(id);
    }

    @Override
    public void revoke(Integer id) {
        userMapper.revoke(id);
    }

    @Override
    public List<User> getUserFriend(Map map) {
        return userMapper.getUserFriend(map);
    }

    private static List<LocalDate> getThisWeekDates() {
        List<LocalDate> dates = new ArrayList<>();
        LocalDate today = LocalDate.now();
        DayOfWeek dayOfWeek = today.getDayOfWeek();
        int daysUntilMonday = dayOfWeek.getValue() - DayOfWeek.MONDAY.getValue();
        if (daysUntilMonday < 0) {
            daysUntilMonday += 7;
        }
        LocalDate monday = today.minusDays(daysUntilMonday);
        for (int i = 0; i < 7; i++) {
            dates.add(monday.plusDays(i));
        }
        return dates;
    }
}
